﻿using System.Collections.Generic;
using SpCop.Core.Model;
using SpCop.Model;

namespace SpCop.General.Scopes
{
    public class DoNotActivateFarmOrWebApplicationFeaturesAutomatically : RuleBase
    {
        public override IList<Model.Problem> Check(Package package)
        {
            var _problems = base.Check(package);
            VisitCollection(package.Features, Visit);
            return _problems;
        }

        public override void Visit(Feature item)
        {
            var scope = item.Scope.ToString();
            if(item.Scope >= Scope.WebApplication)
            {
                var autoActivateDefault = item.Xml.Attribute("ActivateOnDefault");
                if(autoActivateDefault == null)
                {
                    AddProblem("AutoActivateOnDefault", item.Title, scope);
                }else if(autoActivateDefault.Value.ToUpperInvariant() == "TRUE")
                {
                    AddProblem("AutoActivateOnDefaultTrue", item.Title, scope);
                }

                var autoActivateInCentralAdmin = item.Xml.Attribute("AutoActivateInCentralAdmin");
                if (autoActivateInCentralAdmin == null)
                {
                    AddProblem("AutoActivateInCentralAdmin", item.Title, scope);
                }
                else if (autoActivateDefault.Value.ToUpperInvariant() == "TRUE")
                {
                    AddProblem("AutoActivateInCentralAdminTrue", item.Title, scope);
                }
            }
            // dont go deeper
            // base.Visit(item);
        }
    }
}